package io.renren.modules.sys.service.impl;

import com.mchange.v1.util.ListUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.DormViolationDao;
import io.renren.modules.sys.entity.DormViolationEntity;
import io.renren.modules.sys.service.DormViolationService;


@Service("dormViolationService")
public class DormViolationServiceImpl extends ServiceImpl<DormViolationDao, DormViolationEntity> implements DormViolationService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DormViolationEntity> wrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        if (StringUtils.isNotBlank(key)){
            List<SysUserEntity> list = sysUserService.list(new QueryWrapper<SysUserEntity>().like(StringUtils.isNotBlank(key), "real_name", key).or().like(StringUtils.isNotBlank(key), "num", key));
            if (list.isEmpty()){
                wrapper.eq("user_id", 0);
            }else {
                List<Long> collect = list.stream().map((entity) -> {
                    return entity.getUserId();
                }).collect(Collectors.toList());
                wrapper.in(!collect.isEmpty(),"user_id",collect);
            }

        }
        IPage<DormViolationEntity> page = this.page(
                new Query<DormViolationEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}